package com.zq.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqManualConfig {
    private static final Logger logger= LoggerFactory.getLogger(RabbitmqManualConfig.class);

    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;
    @Bean("manualQueue")
    public Queue manualQueue(){
        return new Queue("manualQueue");
    }

    @Bean("manualEx")
    public DirectExchange manualEx(){
        return new DirectExchange("manualEx");
    }

    @Bean
    public Binding binding(){
        return BindingBuilder.bind(manualQueue()).to(manualEx()).with("manualQueue");
    }
    @Bean
    public RabbitTemplate rabbitTemplate(){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                logger.info("交换机已经接受到了");
                if(correlationData!=null){
                    logger.info(correlationData.toString());
                }

                logger.info(""+b);
                logger.info(s);
                logger.info("--------------------------");
            }
        });
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                logger.info("队列接受失败");
                logger.info(message.toString());
                logger.info(i+"");
                logger.info(s);
                logger.info(s1);
                logger.info(s2);
                logger.info("========================");
            }
        });
        return rabbitTemplate;
    }

}
